<?php

defined('BASEPATH') or die('forbidden');

class returnequipment_controller extends Controller {
    
    private $inc;

    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));

        $this->inc=$this->config['inc'];
    }
    
    function index()
    {        
        //print_r($_SESSION['oppop']);
        //var_dump($_SESSION['oppop']);
        //print_r($this->inc['operationResult']);
        $this->view->display("seal/returnequipment/index");
    }
    
     function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        
        $rows = array();
        $name=$_POST['query'];
        if(!isset($name))
        {
            $where.=" and name='".$name."'";
        }
        $sql = "SELECT COUNT(*) AS num FROM return_equipment WHERE {$where}";
        $one = $this->db->one($sql);
        $total = $one['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
        $this->db->query("SELECT {$colsinfo} FROM return_equipment WHERE {$where} {$orderby} LIMIT {$offset},{$rp}");
        while ($n = $this->db->get_array('default')) {
            $n['intime']=  get_datetime($n['intime']);
            $rows[] = array('id' => $n['id'], 'cell' => ($n));
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }
    
    function add() {
        $arealist=get_area_list_host();
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $area_host = $rowdata['area_host'];
            unset($rowdata['area_host']);
          
            if (empty($rowdata['receiver_name'])) {
                $url = $area_host . 'role_name=' . $rowdata['role_name'] . '&action=37';                
                $strResult = vpost($url);
                //$_SESSION['oppop']=$strResult;
                $result=0;
                if ($strResult != 0 || $strResult === FALSE) {
                    $result=1;
                } else {
                    $_SESSION['role_name']=$rowdata['role_name'];   
                    $_SESSION['returnarea_host']=$area_host;
                    insert_log(translate('MENU_SEAL_RETURNEQUIPMENT'),translate('ADD'),NULL,array("name"=>$rowdata['role_name']));
                }
                if($strResult===FALSE)
                {
                    $result=  translate('C_CONNECT_AREA_FIALED');
                }
                else
                {
                    $result=$this->inc['operationResult'][$strResult];
                }
                echo "<script>alert('".$result."');</script>";
            }
            else
            {
                unset($rowdata['role_name']);
                $url=$area_host.array_to_stringparam($rowdata)."&action=34";              
                $strResult=  vpost($url);
                //$_SESSION['oppop']=$strResult;
                $result=0;
                if($strResult!=0||$strResult===FALSE)
                {
                    $result=1;
                }
                else {
                    insert_log(translate('MENU_SEAL_RETURNEQUIPMENT'),translate('ADD'),NULL,array("name"=>$rowdata['receiver_name']));
                    $rowdata['intime']=time();
                    $rowdata['area_host']=$area_host;               
                    $rowdata['name']=$arealist[$rowdata['area_host']];
                    $this->db->insert('return_equipment',$rowdata);
                }
               cp_tips($result == 0 ? "success" : "error");
            }           
           
        }
        $this->view->assign('role_name', $_SESSION['role_name']);
        $this->view->assign("arealist", $this->form->select("row[area_host]", $arealist, $_SESSION['returnarea_host'], array('id' => 'area_host')));
        $this->view->display("seal/returnequipment/add");
    }

}

?>
